var gk = document.getElementsByName("flexRadioDefault");
  let setgk = "";
var flag=1;
  for (let i = 0; i < gk.length; i++) {
    gk[i].addEventListener("change", function () {
      if (gk[i].checked) {
        setgk = gk[i].value;
        console.log(gk[i].value);
        flag--;
      }
    });
}
$(".userdk").click(function () {
  var upload = document.querySelector("#recordFormFile");
  if(flag==1){
    alert("请选择是否公开");
    return;
  }
  if(!upload.files[0]){
    alert("请上传图片");
    return;
  }
  const fileMaxSize = 1024;//1M
  var fileSize = upload.files[0].size;
  var size = fileSize / 1024;
  /*if(size > fileMaxSize){
    alert("文件大小不能超过1M");
  }*/
  const isImg = ['image/jpg', 'image/png', 'image/jpeg','image/gif','image/bmp'].indexOf(upload.files[0].type) > -1;
  if(!isImg){
    console.log("请上传jpg/png/jpeg/gif/bmp文件");
    return;
  }

  const file = upload.files[0];
  if(size > fileMaxSize){
    // 讀取圖片文件
    //console.log(file);
    //alert("before:"+size);
    const reader = new FileReader();
    reader.readAsDataURL(file);

    reader.onload = function() {

      // 將DataURL轉換為圖片對象
      const img = new Image();
      img.src = reader.result;
      
      img.onload = function() {
        // 壓縮圖片
        const canvas = document.createElement('canvas');
        const ctx = canvas.getContext('2d');
        canvas.width = img.width / 2;
        canvas.height = img.height / 2;
        ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
        canvas.toBlob(function(blob) {
          // 使用FormData對象上傳Blob對象到後端
          const formData = new FormData();
          formData.append("pub", setgk);
          formData.append("name", localStorage.getItem("name"));
          formData.append("majority", $(".textbz").val());
          formData.append("photo", blob, 'compressed.jpg');
          //alert("after:"+blob.size/1024);
          axios.post("http://124.71.207.55:8081/addPostByName", formData)
          .then(function (response) {
            console.log("图片上传成功", response);
            if (response.status == 200) {
              alert("打卡成功");
            }
          })
          .catch(function (error) {
            console.log("文件上传失败", error);
          });
          
        }, 'image/jpeg', 0.7);
      };
      
    };
  }else{
    const formData = new FormData();
    formData.append("pub", setgk);
    formData.append("name", localStorage.getItem("name"));
    formData.append("majority", $(".textbz").val());
    formData.append("photo", upload.files[0]);
    axios
          .post("http://124.71.207.55:8081/addPostByName", formData)
          .then(function (response) {
            console.log("图片上传成功", response);
            if (response.status == 200) {
              alert("打卡成功");
            }
          })
          .catch(function (error) {
            console.log("文件上传失败", error);
          });
  }
    
  
  /*
  */
})

/*
fileInput.addEventListener('change', function() {
  const file = fileInput.files[0];
  const image = new Image();

  // 讀取圖片文件
  const reader = new FileReader();
  reader.readAsDataURL(file);
  reader.onload = function(event) {
    image.src = event.target.result;

    // 使用Canvas API壓縮圖片
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');
    canvas.width = image.width;
    canvas.height = image.height;
    ctx.drawImage(image, 0, 0, image.width, image.height);
    canvas.toBlob(function(blob) {
      // 上傳壓縮後的圖片
      const formData = new FormData();
      formData.append('file', blob, file.name);
      // 發送POST請求上傳圖片
      fetch('/upload', {
        method: 'POST',
        body: formData
      });
    }, 'image/jpeg', 0.7);
  }
});*/

